package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.notify.api.SysNotifyTemplateService;
import com.nidu.demo.notify.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 站内信模板管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 站内信模板")
@RestController
@RequestMapping("/system/notify-template")
@Validated
@RequiredArgsConstructor
public class SysNotifyTemplateController {

    private final SysNotifyTemplateService notifyTemplateService;

    @Operation(summary = "创建站内信模板", description = "创建新的站内信模板")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:notify-template:create')")
    public Response addNotifyTemplate(@RequestBody NotifyTemplateAddCmd cmd) {
        return notifyTemplateService.add(cmd);
    }

    @Operation(summary = "更新站内信模板", description = "更新站内信模板的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notify-template:update')")
    public Response updateNotifyTemplate(@PathVariable Long id, @RequestBody NotifyTemplateUpdateCmd cmd) {
        cmd.setId(id);
        return notifyTemplateService.update(cmd);
    }

    @Operation(summary = "删除站内信模板", description = "根据 ID 删除站内信模板")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notify-template:delete')")
    public Response deleteNotifyTemplate(@PathVariable Long id) {
        return notifyTemplateService.delete(id);
    }

    @Operation(summary = "批量删除站内信模板", description = "根据 ID 列表删除多个站内信模板")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:notify-template:delete')")
    public Response deleteNotifyTemplates(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return notifyTemplateService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取站内信模板详情", description = "根据 ID 获取站内信模板详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public SingleResponse<NotifyTemplateCO> getNotifyTemplateById(@PathVariable Long id) {
        return notifyTemplateService.getById(id);
    }

    @Operation(summary = "查询站内信模板列表", description = "获取所有站内信模板信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public MultiResponse<NotifyTemplateCO> listNotifyTemplates(NotifyTemplateListQry qry) {
        return notifyTemplateService.list(qry);
    }

    @Operation(summary = "分页查询站内信模板", description = "分页获取站内信模板信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:notify-template:query')")
    public PageResponse<NotifyTemplateCO> pageNotifyTemplates(NotifyTemplatePageQry qry) {
        return notifyTemplateService.page(qry);
    }

}
